home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 610 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  62 lines

  1. Path: eua.ericsson.se!usenet
  2. From: euahjn@eua.ericsson.se (Henrik Johansson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating a pointer to a function "void (*ptrFunction)()"  inside a class
  5. Date: 5 Jan 1996 11:01:52 GMT
  6. Organization: Ericsson Telecom Systems Labs, Stockholm, Sweden
  7. Message-ID: <4cj0f0$ifu@euas20.eua.ericsson.se>
  8. References: <30ECA10F.3D99@ifu.net>
  9. Reply-To: euahjn@eua.ericsson.se
  10. NNTP-Posting-Host: euas31i2c37.eua.ericsson.se
  11.  
  12. In article <30ECA10F.3D99@ifu.net>, Jason Gresh <gresh@ifu.net> writes:
  13. > Hi,
  14. >     I am trying to create a base class that has a function that the 
  15. > derived class needs to create.  In order to do this, I want to create a 
  16. > pointer to a function in the base class that the derived class can then 
  17. > define.  This is not a case where an override will work because the 
  18. > number and names of the derived functions is not known.  Hopefully this 
  19. > code sample will make this clearer:
  20. [snip]
  21. > If I don't make myFunction part of the derived class (global), 
  22. > everything works fine.  As soon as I include it in the class, I cannot 
  23. > assign a pointer to it.  I am aware that pointers to functions inside 
  24. > the class include the class name in some way ( this is not an issue for 
  25. > global functions).  What is the casting (or other) mechanism to make
  26. > this work?
  27. > Thanks,
  28. >     Mike Gresh
  29.  
  30. How about something like this:
  31.  
  32. class DerivedClass;
  33. class BaseClass
  34. {
  35. public:
  36.         // int (*ptrFunction)();
  37.         int(DerivedClass::*ptrFunction)(int, int);
  38. };
  39.  
  40. class DerivedClass : BaseClass
  41. {
  42.         DerivedClass::DerivedClass();
  43.         int myFunction(int, int);
  44. };
  45.  
  46. DerivedClass::DerivedClass()
  47. {
  48.         ptrFunction = myFunction;
  49. }
  50.  
  51. DerivedClass::myFunction(int, int)
  52. {
  53. // code ....
  54. return 0;
  55. }
  56.  
  57.